package xyf.p07.binsearch;

/**
 * ClassName: P0033_Search
 * Description:
 * Author: xyf
 * Date: 9/7/22 7:06 PM
 * Version: 1.0
 **/
public class P0033_Search
{
    public int search(int[] nums, int target)
    {
        int start = 0;
        int end = nums.length - 1;

        while (start <= end)
        {
            int middle = (end - start) / 2 + start;

            if (nums[middle] == target)
            {
                return middle;
            }
            else if (nums[start] <= nums[middle])
            {
                if (target >= nums[start] && target <= nums[middle])
                {
                    end = middle - 1;
                }
                else
                {
                    start = middle + 1;
                }
            }
            else
            {
                if (target >= nums[middle] && target <= nums[end])
                {
                    start = middle + 1;
                }
                else
                {
                    end = middle - 1;
                }
            }
        }

        return -1;
    }
}